package home_8_25.view.impl;

import home_8_25.dao.BusinessDao;
import home_8_25.dao.FoodDao;
import home_8_25.dao.impl.BusinessDaoImpl;
import home_8_25.dao.impl.FoodDaoImpl;
import home_8_25.view.UserView;

import java.util.List;
import java.util.Map;
import java.util.Scanner;

import static home_8_25.util.ReadText.getIn;

public class BusinessView extends UserView {

    private String busname;
    BusinessView businessView = null;


    public String getBusname() {
        return busname;
    }

    public String setBusname(String busname) {
        this.busname = busname;
        return busname;
    }
    private BusinessDao businessDao = new BusinessDaoImpl();
    private FoodDao foodDao = new FoodDaoImpl();
    @Override
    public Map<String, Object> login() {
        Map<String,Object> map = null;
        Scanner in = new Scanner(System.in);
        System.out.println("商家名称");
        String name = setBusname(in.next());
        System.out.println("商家密码");
        String pwd = in.next();
        map = businessDao.login(busname,pwd);
        return map;
    }

    @Override
    public void menu() {
        System.out.println("商家后台管理功能");
        System.out.println("1.查询食品");
        System.out.println("2.添加食品");
        System.out.println("3.删除食品");
        System.out.println("4.修改食品");
        System.out.println("5.修改商家信息");
        System.out.println("6.记账本功能");

        int n = getIn().nextInt();

        switch (n){
            case 1:
                searchFoodMenu();
                break;
            case 2:
                insertFoodMenu();
                break;

            case 3:
                break;
            case 4:
                editFood();
                break;
            case 5:

                editBusiness();

                break;
            case 6:

                businessView = new AccountView();

                break;
            default:
                System.out.println("请输入正确指令");
        }

    }




    private void editBusiness() {

        //输出原本的信息；
        String name = getBusname();
        List<Map<String,Object>> list =  businessDao.findByName2(name);


        System.out.println("商家编号\t商家名称\t商家地址\t起送费\t 配送费\t状态\t");
        for (int i = 0; i < list.size(); i++) {
            Map<String,Object> map = list.get(i);
            Object id = map.get("busid");
            Object address = map.get("busaddress");
            Object starprice = map.get("bussprice");
            Object deliverprice = map.get("bus_deliveryprice");
            Object status = map.get("business_status");
            System.out.println(id+"\t"+name+"\t"+address+"\t"+starprice+"\t"+deliverprice+"\t"+status);
        }


        //再进行修改
        System.out.println("是否修改：1.是\t2.否");
        int n = getIn().nextInt();
        a:if (n==1){
            System.out.print("商家地址");
            String address  = getIn().next();
            System.out.print("起送费");
            String starprice = getIn().next();
            System.out.print("配送费");
            String deliverprice  = getIn().next();
            System.out.print("经营状态：1.营业2.打烊");
            String status = getIn().next();
            int r = businessDao.editBusiness (address,starprice,deliverprice,status,name);
            if (r > 0 ) {
                System.out.println("成功");
            }
        }else if (n==2){
            return;
        }else {
            System.out.println("请输入正确指令");
            return;
        }
    }

    private void editFood() {
        System.out.println("请输入需要修改的商品的编号");
        String foodId = getIn().next();
        //查询该编号对应的商品信息；
        String id = getInfo().get("busid").toString();
        Map<String,Object> map = foodDao.findFoodById(foodId,id);

        if (map==null){
            System.out.println("编号有误");
            return;
        }else {

        }
        System.out.println("商品编号\t商品名称\t商品价格\t商品介绍");
        Object name = map.get("fanme");
        Object price = map.get("fprice");
        Object info = map.get("explian");
        System.out.println(foodId+"\t"+name+"\t"+price+"\t"+info);



        //再进行修改
        System.out.println("是否修改：1.是\t2.否");
        int n = getIn().nextInt();
        a:if (n==1){
            System.out.print("商品名称");
            name = getIn().next();
            System.out.print("商品价格");
            price = getIn().next();
            System.out.print("商品介绍");
            info  = getIn().next();

            int r = foodDao.editFood((String) name,price.toString(),(String) info,id);
            if (r > 0 ) {
                System.out.println("成功");
            }

        }else if (n==2){
            return;
        }else {
            System.out.println("请输入正确指令");
            return;
        }




    }

    private void searchFoodMenu() {

        System.out.println("1.查询所有商品名");
        System.out.println("2.按照商品类型查询");
        System.out.println("请选择功能");
        int n = getIn().nextInt();
        switch (n){
            case 1:
                searchFoodAll();
                break;
            case 2:
                searchFoodByName();
                break;
            default:
                System.out.println("请输入正确指令");
        }
    }

    private void searchFoodByName() {
        String n = getIn().next();
        List<Map<String,Object>> list = foodDao.findByName(n);
        System.out.println("ID" +"\t"+ "NAME"+"\t" +"描述"+"\t" +"价格"+"\t" +"状态");
        for (int i = 0; i < list.size(); i++) {
            Map<String,Object> map = list.get(i);
            Object fid = map.get("fid");
            Object name = map.get("fname");
            Object explian = map.get("fexplain");
            Object price = map.get("fsprice");
            Object status = map.get("food_status");
            System.out.println(fid+"\t"+name+"\t"+explian+"\t"+price+"\t"+status);
        }

    }

    private void  searchFoodAll() {

        //从登陆成功后存放点用户信息中获取商家的ID。

        String id = getInfo().get("busid").toString();
        List<Map<String,Object>> list = foodDao.finAll(id);
        System.out.println("ID" +"\t"+ "NAME"+"\t" +"描述"+"\t" +"价格"+"\t" +"状态");
        for (int j = 0; j < list.size(); j++) {
            Map<String,Object> map = list.get(j);
            Object fid = map.get("fid");
            Object name = map.get("fname");
            Object explian = map.get("fexplain");
            Object price = map.get("fsprice");
            Object status = map.get("food_status");
            System.out.println(fid+"\t"+name+"\t"+explian+"\t"+price+"\t"+status);
        }
    }



    private void insertFoodMenu() {

        System.out.println("1.添加");
        System.out.println("2.商品类型添加");
        System.out.println("请选择");
        int n = getIn().nextInt();
        switch (n){
            case 1:
                insertFood();
                break;
            case 2:

                break;
            default:
                System.out.println("请输入正确指令");
        }
    }

    private void insertFood() {

        System.out.println("请输入商品的信息");
        System.out.println("商品的名称：");
        String name = getIn().next();
        System.out.println("商品的价格：");
        String price = getIn().next();
        System.out.println("商品的介绍：");
        String info = getIn().next();

        //获取当前登录的商家的编号
        String id = getInfo().get("busid").toString();
        int row = foodDao.insertFood(name,price,info,id);
        if (row > 0) {
            System.out.println("添加成功");
        }else {
            System.out.println("添加失败");
        }

    }

}
